package com.iiapk.gridnew;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;

import com.iiapk.grid.database.ColumnTypeManager;
import com.iiapk.gridnew.domain.ColumnType;
import com.iiapk.gridnew.fragment.GridFragment;
import com.iiapk.gridnew.view.AnimButtons;
import com.iiapk.gridnew.view.DotPoint;
import com.iiapk.gridnew.view.AnimButtons.OnButtonClickListener;

public class GridPagerActivity extends FragmentActivity {
	
	private FragmentPagerAdapter gridPagerAdapter;
	private ViewPager gridPager;
	private ArrayList<Fragment> pageviews= new ArrayList<Fragment>();
	private DotPoint dot;
	private ColumnTypeManager columnTypeManager;
	 private LoadImageTask loadImageTask;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		columnTypeManager = new ColumnTypeManager(this);
		initList();
		
		AnimButtons animButtons = (AnimButtons)findViewById(R.id.main_in2);
		animButtons.setOnButtonClickListener(new OnButtonClickListener(){
			@Override
			public void onButtonClick(View v, int id) {
				if(v.getId()==R.id.btn_place){
					Intent intent = new Intent(GridPagerActivity.this,DragListTypeActivity.class);
					startActivity(intent);
					overridePendingTransition(R.anim.push_up_in,R.anim.hold);
				}else if(v.getId()==R.id.btn_with){
					Intent intent = new Intent(GridPagerActivity.this, SettingPreferences.class);
					startActivity(intent);
					overridePendingTransition(R.anim.push_up_in,R.anim.hold);
				}else if(v.getId()==R.id.btn_camera){
					Intent intent = new Intent(GridPagerActivity.this,FavoriteArticleActivity.class);
					startActivity(intent);
					overridePendingTransition(R.anim.push_up_in,R.anim.hold);
				}else{}
			}
		});
		
		/*findViewById(R.id.main_deleteBT).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(GridPagerActivity.this,FavoriteArticleActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.push_up_in,R.anim.hold);
			}
		});
		findViewById(R.id.main_addBT).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(GridPagerActivity.this,DragListTypeActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.push_up_in,R.anim.hold);
			}
		});
		findViewById(R.id.main_settingBT).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(GridPagerActivity.this, SettingPreferences.class);
				startActivity(intent);
				overridePendingTransition(R.anim.push_up_in,R.anim.hold);
			}
		});*/
		dot = (DotPoint)findViewById(R.id.grid_dis);
		dot.initViewSize(pageviews.size());
		
		gridPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager());
		
		gridPager = (ViewPager) findViewById(R.id.main_gridpager);
		gridPager.setAdapter(gridPagerAdapter);
		gridPager.setCurrentItem(1);
		
		gridPager.setOnPageChangeListener(new OnPageChangeListener(){
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}
			@Override
			public void onPageSelected(int arg0) {
				if(arg0 == 0)  
					gridPager.setCurrentItem(arg0 + 1);  
				else if(arg0 == pageviews.size() - 1)  
					gridPager.setCurrentItem(arg0 - 1); 
				else
					dot.setSelectItem(arg0);
			}
		});
		beginLoadImageTask();
	}
	
	private void beginLoadImageTask() {
		if (loadImageTask != null) {
			loadImageTask.cancel(true);
		}
		loadImageTask = new LoadImageTask();
		loadImageTask.execute();
	}
	
	public void initList(){
		List<ColumnType> list = columnTypeManager.select();
		List<ColumnType> _list=null;
		int _index=0;
		pageviews.clear();
		pageviews.add(new Fragment());
		if(list.size()<=6){
			pageviews.add(new GridFragment(list));
		}else{
			while(list.size()>6){
				_list = new ArrayList<ColumnType>(list.subList(_index, _index+6));
				pageviews.add(new GridFragment(_list));
				list=new ArrayList<ColumnType>(list.subList(_index+6,list.size()));
				_index+=6;
			}
			pageviews.add(new GridFragment(list));
		}
		pageviews.add(new Fragment());
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		initList();
		gridPagerAdapter.notifyDataSetChanged();
	}
	
	@Override
	protected void onRestart() {
		super.onRestart();
		initList();
		gridPagerAdapter.notifyDataSetChanged();
	}

	private class FragmentPagerAdapter extends FragmentStatePagerAdapter {
		public FragmentPagerAdapter(FragmentManager fm) {
			super(fm);
		}
		@Override
		public int getCount() {
			return pageviews.size();
		}
		@Override
		public Fragment getItem(int position) {
			return pageviews.get(position);
		}
		@Override
		public int getItemPosition(Object object) {
			return POSITION_NONE;
		}
	}
	
	class LoadImageTask extends AsyncTask<Integer, Void, Void> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}
		@Override
		protected Void doInBackground(Integer... params) {
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
		}
	}
}